package mi.payment.transaction.authorize.complete;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import mi.payment.R;
import mi.payment.bean.db.TradeData;
import mi.payment.constants.Params;
import mi.payment.db.dao.PayDetail;
import mi.payment.transaction.authorize.base.PreAuthorizeInputInfoActivity;
import sunmi.payment.L3.L3Params;

/**
 * 预授权完成模块 -- 输入信息界面
 *
 * @author by lj on 2017/6/22.
 */
public class PreAuthorizeCompleteDealInfoActivity extends PreAuthorizeInputInfoActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        mAmountView.setVisibility(View.GONE);
        mTvTitle.setText(R.string.pre_auth_complete);

        // 外调程序填入数据
        L3Params l3Params = Params.sL3Params;
        if (l3Params == null) return;
        String oriAuthNo = l3Params.oriAuthNo;
        if (oriAuthNo != null && oriAuthNo.length() > 0) {
            mTvOrderNo.setText(oriAuthNo);
        }
        String date = l3Params.oriTransDate;
        if (date != null && date.length() > 0) {
            mTvDate.setText(date);
        }
    }

    @Override
    public void onClick(View v) {
        // super.onClick(v) 不能去掉 父类有点击事件
        super.onClick(v);
        final int id = v.getId();
        switch (id) {
            case R.id.mb_ok:
                String authNo = mTvOrderNo.getText().toString();
                String date = mTvDate.getText().toString().replaceAll("/", "");
                if (authNo.length() != 6) {
                    showToast(R.string.error_input_auth_number_hint);
                    return;
                }
                if (date.length() != 4) {
                    showToast(R.string.error_input_date_hint);
                    return;
                }

                TradeData tradeData = Params.getTradeData();
                PayDetail payDetail = tradeData.getPayDetail();
                payDetail.originalAuthNo = authNo;
                payDetail.originalTransDate = date;
                PayDetail originalPayDetail = mPayDetailModel.getVoucherNoByCardNOAndAuthNo(payDetail.CardNo, authNo);
                if (originalPayDetail != null) {
                    payDetail.originalPOSNum = originalPayDetail.voucherNo;
                }
                tradeData.setPayDetail(payDetail);

                PreAuthorizeCompleteInputMoneyActivity.startAction(this);
                finish();
        }
    }

    public static void startAction(Context context) {
        Intent intent = new Intent(context, PreAuthorizeCompleteDealInfoActivity.class);
        context.startActivity(intent);
    }


}
